package com.ygp.dtp.core.support;

import com.ygp.dtp.core.DynamicThreadPoolRegistry;
import com.ygp.dtp.core.thread.DynamicThreadPoolExecutor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * @description: 处理Spring管理的所有相关bean的BeanPostProcessor，主要是指@Bean注解标记的bean。
 * @motto: 代码源于生活，高于生活艺术
 * @author: zhouhengzhe
 * @date: 2022/8/10 11:01
 * @since 1.0
 **/
public class DynamicThreadPoolBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof DynamicThreadPoolExecutor) {
            DynamicThreadPoolExecutor executor = (DynamicThreadPoolExecutor) bean;
            register(executor);
        }
        return null;
    }

    /**
     * 注册线程池
     *
     * @param executor
     */
    private void register(DynamicThreadPoolExecutor executor) {
        DynamicThreadPoolRegistry.register(executor, "beanPostProcessor");
    }
}
